﻿using System;
using System.Data.OleDb;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.IO;
using System.Text;
using Dukey.DBUtility;
using System.Collections;


public partial class Crm_EditFormColumn : BaseCrm
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (!StringHelper.IsBadChar(Request.QueryString["id"]))
            {
                string id = Request.QueryString["id"];
                DataTable dt = BLL.DataBaseHelper.instance.GetModel("FormParas", "*", "[ID]=" + id);
                if (dt.Rows.Count > 0)
                {
                    BindShowType();
                    txtCname.Text = dt.Rows[0]["cname"].ToString();
                    txtName.Text = dt.Rows[0]["para_name"].ToString();
                    dropType.SelectedValue = dt.Rows[0]["datatype"].ToString();
                    txtWidth.Text = dt.Rows[0]["width"].ToString();
                    txtMaxLength.Text = dt.Rows[0]["maxlength"].ToString();
                    txtPx.Text = dt.Rows[0]["px"].ToString();
                    txtDefaultValue.Text = dt.Rows[0]["defaultvalue"].ToString();
                    txtHtmlStr.Text = dt.Rows[0]["htmlstr"].ToString();
                    DataTable dt1=BLL.DataBaseHelper.instance.GetModel("HtmlTags","htmlstr","tagname='"+dropType.SelectedValue+"'");
                    if (dt1.Rows.Count > 0)
                        hidtypestr.Value = dt1.Rows[0]["htmlstr"].ToString();
                }
                
            }
        }
    }




    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        //提交
        try
        {
            string cname = StringHelper.ReplaceBadChar(txtCname.Text);
            string para_name = StringHelper.ReplaceBadChar(txtName.Text);
            string datatype = dropType.SelectedValue;
            int maxlength =0;
            int.TryParse(txtMaxLength.Text,out maxlength);
            int width = 0;
            int.TryParse(txtWidth.Text, out width);
            int px = 0;
            int.TryParse(txtPx.Text, out px);

            string htmlstr = txtHtmlStr.Text;

            Hashtable hs = new Hashtable();
            hs.Add("cname", cname);
            hs.Add("para_name", para_name);
            hs.Add("datatype", datatype);
            hs.Add("maxlength", maxlength);
            hs.Add("width", width);
            hs.Add("px", px);
            hs.Add("htmlstr", htmlstr);
            hs.Add("defaultvalue",txtDefaultValue.Text);
            if (!StringHelper.IsBadChar(Request.QueryString["id"]))
            {
                string id = Request.QueryString["id"];
                BLL.DataBaseHelper.instance.Update(hs, "FormParas", "ID=" + id);
            }
            ClientScript.RegisterClientScriptBlock(this.GetType(), "ok", "<script>Finish();</script>");
        }
        catch (Exception ex)
        {
            Common.MessageBox.Show(this, ex.Message);
        }


    }




    private void BindShowType()
    {
        dropType.DataSource = BLL.DataBaseHelper.instance.GetList("HtmlTags");
        dropType.DataTextField = "cname";
        dropType.DataValueField = "tagname";
        dropType.DataBind();
        //trLength.Visible = true;
        //trDefaultValue.Visible = true;

        //switch (dropType.SelectedValue)
        //{
        //    case "TEXT":
        //        {                   
        //            break;
        //        }
        //    case "COUNTER":
        //        {
        //            txtDefaultValue.Text = "";
        //            trLength.Visible = false;
        //            trDefaultValue.Visible = false;
        //            break;
        //        }
        //    default:
        //        {
        //            txtLength.Text = "";                 
        //            trLength.Visible = false;
        //            break;
        //        }
        //}
    }
    protected void dropType_SelectedIndexChanged1(object sender, EventArgs e)
    {
        DataTable dt1 = BLL.DataBaseHelper.instance.GetModel("HtmlTags", "htmlstr", "tagname='" + dropType.SelectedValue + "'");
        if (dt1.Rows.Count > 0)
            hidtypestr.Value = dt1.Rows[0]["htmlstr"].ToString();
    }
}
